export function isPointInPath({ offsetX, offsetY }, { type, props }) {
  switch (type) {
    case "rect":
    case "stroke":
    case "roundrect":
    case "group":
      return InRect(offsetX, offsetY, props.x, props.y, props.w, props.h);
    case "line":
      return InLine(offsetX, offsetY, props.path, props.lineWidth);
    case "polygon":
      return InPolygon(offsetX, offsetY, props.path);
    case "arc":
      const x = offsetX - props.r;
      const y = offsetY - props.r;
      return InArc(x, y, props.r, props.startAngle, props.endAngle);
    default:
      return false;
  }
}
/**
 * 判断是否在矩形区域
 * @param {} x0
 * @returns
 */
export function InRect(x, y, recX, recY, recW, recH) {
  const xLeft = x > recX;
  const xRight = x < recX + recW;
  const yTop = y > recY;
  const yBottom = y < recY + recH;
  return xRight && xLeft && yTop && yBottom;
}
/**
 * 判断是否在不规则折线区域
 * @param {} x0
 * @returns
 */
export function InLine(offsetX, offsetY, path, lineWidth) {
  const length = path.length;
  for (let i = 0; i < length; i++) {
    if (i > 0) {
      if (
        insideLine(
          path[i - 1][0],
          path[i - 1][1],
          path[i][0],
          path[i][1],
          lineWidth,
          offsetX,
          offsetY
        )
      ) {
        return true;
      }
    }
  }
  function insideLine(x0, y0, x1, y1, lineWidth, x, y) {
    if (lineWidth === 0) {
      return false;
    }
    let _l = lineWidth;
    let _a = 0;
    let _b = x0;
    // Quick reject
    if (
      (y > y0 + _l && y > y1 + _l) ||
      (y < y0 - _l && y < y1 - _l) ||
      (x > x0 + _l && x > x1 + _l) ||
      (x < x0 - _l && x < x1 - _l)
    ) {
      return false;
    }
    if (x0 !== x1) {
      _a = (y0 - y1) / (x0 - x1);
      _b = (x0 * y1 - x1 * y0) / (x0 - x1);
    } else {
      return Math.abs(x - x0) <= _l / 2;
    }
    let tmp = _a * x - y + _b;
    let _s = (tmp * tmp) / (_a * _a + 1);
    return _s <= ((_l / 2) * _l) / 2;
  }
  return false;
}
/**
 * 判断是否在不多边形折线区域
 * @param {} x0
 * @returns
 */
export function InPolygon(x, y, points) {
  let result = false;
  for (let i = 0, j = points.length - 1; i < points.length; j = i++) {
    if (
      points[i][1] > y != points[j][1] > y &&
      x <
        ((points[j][0] - points[i][0]) * (y - points[i][1])) /
          (points[j][1] - points[i][1]) +
          points[i][0]
    ) {
      result = !result;
    }
  }
  return result;
}
/**
 * 判断是否在圆弧区域
 * @param {} x0
 * @returns
 */
export function InArc(x, y, r, startAngle, endAngle) {
  const sa =
    startAngle < 0
      ? 2 * Math.PI + (Math.PI / 180) * startAngle
      : (Math.PI / 180) * startAngle;
  const ea =
    endAngle < 0
      ? 2 * Math.PI + (Math.PI / 180) * endAngle
      : (Math.PI / 180) * endAngle;
  let dis, isIn;
  // Sector
  if (!isNaN(sa) && !isNaN(ea)) {
    let angle = 0;
    // 4th quadrant
    if (x >= 0 && y >= 0) {
      if (x === 0) {
        angle = Math.PI / 2;
      } else {
        angle = Math.atan(y / x);
      }
    }
    // 3th quadrant
    else if (x <= 0 && y >= 0) {
      if (x === 0) {
        angle = Math.PI;
      } else {
        angle = Math.PI - Math.atan(y / Math.abs(x));
      }
    }
    // secend quadrant
    else if (x <= 0 && y <= 0) {
      if (x === 0) {
        angle = Math.PI;
      } else {
        angle = Math.atan(Math.abs(y) / Math.abs(x)) + Math.PI;
      }
    }
    // first quadrant
    else if (x >= 0 && y <= 0) {
      if (x === 0) {
        angle = (Math.PI * 3) / 2;
      } else {
        angle = 2 * Math.PI - Math.atan(Math.abs(y) / x);
      }
    }
    dis = Math.sqrt(x * x + y * y);
    if (sa < ea) {
      isIn = !!(angle >= sa && angle <= ea && dis <= r);
    } else {
      isIn = !!(
        ((angle >= 0 && angle <= ea) ||
          (angle >= sa && angle <= 2 * Math.PI)) &&
        dis <= r
      );
    }
  } else {
    isIn = !!(Math.sqrt(x * x + y * y) <= r);
  }
  return isIn;
}
